
package com.sskj.utils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class AmapHelp {

	/** WEB服务API KEY */
	// @Value("${sskj.amap.webServiceKey}")
	private static final String DEFAULT_WEB_SERVICE_KEY = "1dd08ec0fffc99b1d5cd5cfa0224a924";

	/** 结构化地址转经纬度API */
	// @Value("${sskj.amap.apiUrlGeocodeGeo}")
	private static final String API_URL_GEOCODE_GEO = "http://restapi.amap.com/v3/geocode/geo";

	/** 经纬度转结构化地址API */
	// @Value("${sskj.amap.apiUrlGeocodeRegeo}")
	private static final String API_URL_GEOCODE_REGEO = "http://restapi.amap.com/v3/geocode/regeo";

	/** 非高德坐标（GPS坐标、mapbar坐标、baidu坐标）转换成高德坐标API */
	// @Value("${sskj.amap.apiUrlGeocode}")
	private static final String API_URL_COORDINATE_CONVERT = "http://restapi.amap.com/v3/assistant/coordinate/convert";

	/** 指定查询的城市 */
	// @Value("${sskj.amap.city}")
	private static final String DEFAULT_CITY = "宜昌";

	/** 返回数据格式类型 */
	// @Value("${sskj.amap.output}")
	private static final String OUTPUT = "JSON";

	/**
	 * 结构化地址转经纬度
	 * 
	 * @param address
	 *            结构化地址
	 * @return 经纬度
	 * @throws Exception
	 */
	public static String geo(String key, String address, String city) throws Exception {

		if (StringUtils.isEmpty(address)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("key", StringUtils.isEmpty(key) ? DEFAULT_WEB_SERVICE_KEY : key);
		params.put("address", address);
		params.put("city", StringUtils.isEmpty(city) ? DEFAULT_CITY : city);
		params.put("output", OUTPUT);

		String response = HttpHelp.sendGet(API_URL_GEOCODE_GEO, params);
		if (response != null) {
			JSONObject jsStr = JSONObject.parseObject(response);
			if (jsStr != null && "1".equals(jsStr.getString("status"))) {
				JSONArray geocodes = jsStr.getJSONArray("geocodes");
				if (geocodes != null && geocodes.size() > 0) {
					JSONObject geocode = JSONObject.parseObject(geocodes.get(0).toString());
					String location = geocode.getString("location");
					if (StringUtils.isNotEmpty(location)) {
						return location;
					}
				}
			}
		}

		return null;
	}

	/**
	 * 经纬度转结构化地址
	 * 
	 * @param lng
	 *            坐标点经度
	 * @param lat
	 *            坐标点纬度
	 * @return 结构化地址
	 * @throws Exception
	 */
	public static String regeo(String key, String lng, String lat) throws Exception {

		if (StringUtils.isEmpty(lng) || StringUtils.isEmpty(lat)) {
			return null;
		}
		BigDecimal lngBd;
		BigDecimal latBd;
		try {
			lngBd = new BigDecimal(StringUtils.trim(lng));
			latBd = new BigDecimal(StringUtils.trim(lat));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		String lngStr = lngBd.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
		String latStr = latBd.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("key", StringUtils.isEmpty(key) ? DEFAULT_WEB_SERVICE_KEY : key);
		params.put("location", lngStr + "," + latStr);
		params.put("roadlevel", "0");
		params.put("output", OUTPUT);

		String response = HttpHelp.sendGet(API_URL_GEOCODE_REGEO, params);
		if (response != null) {
			JSONObject jsStr = JSONObject.parseObject(response);
			JSONArray regeocodes = jsStr.getJSONArray("regeocodes");
			if (regeocodes != null && regeocodes.size() > 0) {
				JSONObject geocode = JSONObject.parseObject(regeocodes.get(0).toString());
				String address = geocode.getString("formatted_address");
				if (StringUtils.isNotEmpty(address)) {
					return address;
				}
			}
		}

		return null;
	}

	/**
	 * 非高德坐标（GPS坐标、mapbar坐标、baidu坐标）转换成高德坐标
	 * 
	 * @param lng
	 *            坐标点经度
	 * @param lat
	 *            坐标点纬度
	 * @param coordsys
	 *            原坐标系 (gps;mapbar;baidu;autonavi;)
	 * @return 高德坐标经纬度
	 * @throws Exception
	 */
	public static String coordinateConvert(String key, String lng, String lat, String coordsys) throws Exception {

		if (StringUtils.isEmpty(lng) || StringUtils.isEmpty(lat)) {
			return null;
		}
		BigDecimal lngBd;
		BigDecimal latBd;
		try {
			lngBd = new BigDecimal(StringUtils.trim(lng));
			latBd = new BigDecimal(StringUtils.trim(lat));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		String lngStr = lngBd.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
		String latStr = latBd.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("key", StringUtils.isEmpty(key) ? DEFAULT_WEB_SERVICE_KEY : key);
		params.put("output", OUTPUT);
		params.put("locations", lngStr + "," + latStr);
		if (StringUtils.isNotEmpty(coordsys)) {
			params.put("coordsys", coordsys);
		}

		String response = HttpHelp.sendGet(API_URL_COORDINATE_CONVERT, params);
		if (response != null) {
			JSONObject jsStr = JSONObject.parseObject(response);
			if (jsStr != null && "1".equals(jsStr.getString("status"))) {
				String locations = jsStr.getString("locations");
				if (StringUtils.isNotEmpty(locations)) {
					return locations;
				}
			}
		}

		return null;
	}
}
